import vnode from './vnode';

// 低配版本的h函数，重载功能较弱
/* 
    形态一：h('div', {}, '文字')
    形态二：h('div', {}, [])
    形态三：h('div', {}, h())
*/
export default function (sel, data, c) {
    //  1 检查参数个数
    if (arguments.length != 3) {
        throw new Error('function must have three argument')
    }
    // 2 检查参数 c 的类型
    let type = typeof c
    //  1 常数类型：形态一
    if (type === 'string' || type === 'number') {
        return vnode(sel, data, undefined, c, undefined)
    }
    //  2 数组：形态二
    else if (Array.isArray(c)) {
        let children = []
        c.forEach((v, i) => {
            // * c[i]必须是一个对象
            if (typeof c[i] === 'object' & c[i].hasOwnProperty('sel')) {
                children.push(v)
            }
            else {
                throw new TypeError('array argument internal value must be a object')
            }
        })
        return vnode(sel, data, children, undefined, undefined)
    }
    //  3 对象：形态三
    else if (type === 'object' && c.hasOwnProperty('sel')) {
        return vnode(sel, data, [c], undefined, undefined)
    }
    // 4 无上述类型：抛出错误
    else {
        throw new TypeError('argument is unexpected type')
    }
}